package com.xbongbong.paas.controller.mobile;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.xbongbong.paas.service.PaasFormExplainService;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.formexplain.pojo.dto.LinkFormItemDTO;
import com.xbongbong.pro.formexplain.pojo.vo.LinkFormItemVO;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * 表单解释
 *
 * @author feng.zheng
 * @date 2019/3/19 9:47
 */
@RestController
@RequestMapping(XbbProConstant.API_ROOT_PREFIX + "/mobile/form/explain")
public class MobilePaasFormExplainController {

    @Resource
    private PaasFormExplainService paasFormExplainService;

    /**
     *  客户关联联系人
     * @param linkFormItemDTO 关联新建入参
     * @param br
     * @return
     */
    @RequestMapping(value = "/linkForm/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String linkFormList(@RequestBody @Valid LinkFormItemDTO linkFormItemDTO, BindingResult br) {
        XbbResponse<LinkFormItemVO> response;
        if(br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                LinkFormItemVO linkFormItemVO = paasFormExplainService.linkFormList(linkFormItemDTO);
                response = new XbbResponse<>(linkFormItemVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * Description: 关联新建（工作流）
     * @param linkFormItemDTO
	 * @param br
     * @return java.lang.String
     * @throws
     * @author 魏荣杰
     * @date 2021/7/7 13:47
     * @since
     */
    @RequestMapping(value = "/workflow/linkForm/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String workflowLinkFormList(@RequestBody @Valid LinkFormItemDTO linkFormItemDTO, BindingResult br) {
        XbbResponse<LinkFormItemVO> response;
        if(br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                LinkFormItemVO linkFormItemVO = paasFormExplainService.workflowLinkFormList(linkFormItemDTO);
                response = new XbbResponse<>(linkFormItemVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }
}
